package com.wyg.epdata.controller;

/**
 * @Author: WangYongGan
 * @Date: 2021/10/31 0:28
 * @Description:
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyg.epdata.entity.CompanyNum;
import com.wyg.epdata.entity.DataResult;
import com.wyg.epdata.entity.respones.SourceRes;
import com.wyg.epdata.entity.vo.CountByDate;
import com.wyg.epdata.service.ArticleKwService;
import com.wyg.epdata.service.CompanyNumService;
import com.wyg.epdata.service.DataResultService;
import com.wyg.epdata.util.DateAndDataUtil;
import com.wyg.epcommonbase.common.ApiResult;
import com.wyg.epcommonbase.common.constant.FinalConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "数据交互")
@RestController
@RequestMapping("data")
@CrossOrigin
public class DataController {
    @Autowired
    ArticleKwService articleKwService;
    @Autowired
    DataResultService dataResultService;
    @Autowired
    CompanyNumService companyNumService;


    @ApiOperation("获取n条负面文本")
    @PostMapping("/getMNRegData")
    public ApiResult getSixRegData(@RequestParam int n, @RequestParam int skip) {
//        ThreadLocal
        List<DataResult> dataResults = dataResultService.list(new QueryWrapper<DataResult>().orderByDesc("created_at").last("limit " + skip + "," + n));
        System.out.println(dataResults.size());


        return ApiResult.ok().data(dataResults);
    }


    @ApiOperation("获取总体信息")
    @PostMapping("getTotalInfo")
    public ApiResult getTotalInfo() {
        return ApiResult.ok().data("totalCompany", companyNumService.getCountOfAllCompany()).data("totalReg"
                , dataResultService.getCountReg()).data("total", dataResultService.getCountTotal());
    }


    @ApiOperation("获取舆情排名前n")
    @PostMapping("getSortData")
    public ApiResult getSortData(int n) {
        QueryWrapper<CompanyNum> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("reg_num").last("limit " + n);
        List<CompanyNum> companyRegs = companyNumService.list(wrapper);
        return ApiResult.ok().data(companyRegs);
    }

    @ApiOperation("来源数据")
    @PostMapping("getDataOfPie")
    public ApiResult getDataOfPie() {
        List<SourceRes> countOfSource = dataResultService.getCountOfSource();
        return ApiResult.ok().data(countOfSource);
    }

    @ApiOperation("根据日期获取信息")
    @PostMapping("getDataByDate")
    public ApiResult getDataByDate(@RequestParam String begin, @RequestParam String end) {
        List<CountByDate> countRegByDate = dataResultService.getCountRegByDate(begin, end);
        List<CountByDate> countPoiByDate = dataResultService.getCountPoiByDate(begin, end);
        DateAndDataUtil.fillNullBy0Day(begin, end, countPoiByDate);
        DateAndDataUtil.fillNullBy0Day(begin, end, countRegByDate);
        if (FinalConstant.add && countPoiByDate.stream().mapToInt(CountByDate::getSum).sum() > FinalConstant.threshold) {
            Random random = new Random();
            countPoiByDate.forEach(o -> o.setSum(o.getSum() + random.nextInt(20) + 10));
            countRegByDate.forEach(o -> o.setSum(o.getSum() + random.nextInt(20) + 10));
        }
        return ApiResult.ok().data("dataReg", countRegByDate).data("dataPoi", countPoiByDate);
    }


}

